package com.itlong.cloud.controller.sharedELevator;

import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.project.property.IPropertyCommunityInfoServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      收费配置Controller
 * </desc>
 *
 * @createDate 2020/07/15
 */
@RestController
@RequestMapping("/sharedElevator")
@RefreshScope
public class FeeConfigController {

    @Autowired
    IPropertyCommunityInfoServiceClient iPropertyCommunityInfoServiceClient;

    /**
     * <desc>
     *     保存收费间隔时间
     * <desc/>
     *
     * @param  projectId  项目编号
     * @param  intervalTime  间隔时间
     * @param  type  类型 1保存 2获取
     * @param  warningTimes  预警次数
     * @return  MessageVO 标准格式化响应结果
     * @author JuGuang.S
     * @createDate 2020/07/15
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping(path = "/feeConfig")
    public Object saveFeeIntervalTime(String projectId,String intervalTime,Integer type,Integer warningTimes) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            projectId = request.getAttribute("projectId") == null ? null : request.getAttribute("projectId").toString();
            intervalTime = request.getAttribute("intervalTime") == null ? null : request.getAttribute("intervalTime").toString();
            type = request.getAttribute("type") == null ? null : Integer.parseInt(request.getAttribute("type").toString());
            warningTimes = request.getAttribute("warningTimes") == null ? null : Integer.parseInt(request.getAttribute("warningTimes").toString());
            if (StringUtils.isEmpty(projectId) || type == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }

            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyCommunityInfoServiceClient.saveFeeIntervalTime(projectId,intervalTime,type,warningTimes));
        } catch (Exception e) {
            throw new DataAccessException("【物业_共享电梯收费配置】保存收费间隔时间失败", e);
        }
    }

}